﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDT.Model;
using SDT.Lib.Common;
using SDT.Lib.Paging;
using SDT.Admin.ViewModels;
using System.Web.Security;
using SDT.Admin.Properties;
using SDT.Lib.Helpers;
namespace SDT.Admin.Controllers
{
    [RoleHelper(Roles = RoleSystem.Dictionary)]
    public class DemandConvertPriceController : Controller
    {
        /// <summary>
        /// Created by: VietLv
        /// Created date: 01/08/2011
        /// Description: Manage demand convertePrice
        /// </summary>

        ChoVietDataContext eDataContext = new ChoVietDataContext();

        public ActionResult Index(int? page)
        {
            try
            {
                if (!page.HasValue) page = 1;
                string keyword = UserState.getUserStateFromRequest("convertPrice.Keyword", "Keyword", "").ToString();
                int pageSize = int.Parse(UserState.getUserStateFromRequest("convertPrice.pageSize", "PageSize", 20).ToString());
                int TypeProductID = Convert.ToInt32(UserState.getUserStateFromRequest("convertPrice.TypeID", "TypeID", 0));
                var Demand_Type = eDataContext.DemandTypes.ToList();
                var Price = eDataContext.DemandConvertPrices
                   .Where(p => (TypeProductID > 0 ? p.TypeID == TypeProductID : true))
                   .Where(p => (string.IsNullOrEmpty(keyword) ? true :
                                (p.ID.ToString().ToLower().Contains(keyword.ToLower().Trim())
                                ))).ToPagedList(page.Value - 1, pageSize);

                return View(new DemandConvertPriceViewModels
                {
                    ListDemandConvertPrice = Price,
                    Keyword = keyword,
                    DemandTypes = Demand_Type,
                    CurrentTypeProduct = TypeProductID
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region POST: /ConvertPrice/Create
        public ActionResult Create()
        {
            try
            {
                var ListType = eDataContext.DemandTypes.ToList();
                return View(new DemandCreatePriceViewModels { DemandTypes = ListType });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region POST: /ConvertPrice/Create
        [HttpPost]
        public ActionResult Create(DemandCreatePriceViewModels cp)
        {
            try
            {
                UpdateModel(cp);
                var CurrentConvertPrice = new DemandConvertPrice();
                CurrentConvertPrice.AddBy = UserManager.GetUserId();
                CurrentConvertPrice.AddDate = DateTime.Now;
                CurrentConvertPrice.TypeID = cp.CurrenTypeID;
                CurrentConvertPrice.Name = cp.Name;
                if (cp.Min > 0 && cp.Max > 0 && cp.Min > cp.Max)
                {

                    ViewData["MessageInfo"] = "Dữ liệu bị lỗi bạn hãy xem lại dữ liệu đầu vào";
                    return View();
                }
                if (cp.Min < 0 || cp.Max < 0)
                {
                    ViewData["MessageInfo"] = "Dữ liệu bị lỗi bạn hãy xem lại dữ liệu đầu vào";
                    return View();
                }
                CurrentConvertPrice.MinValue = cp.Min;
                CurrentConvertPrice.MaxValue = cp.Max;
                eDataContext.DemandConvertPrices.InsertOnSubmit(CurrentConvertPrice);
                eDataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewData["MessageInfo"] = "Dữ liệu bị lỗi bạn hãy xem lại dữ liệu đầu vào";
                return View();
            }
        }
        #endregion

        #region GET: /ConvertPrice/Edit
        public ActionResult Edit(int id)
        {
            try
            {
                if (id < 0) throw new NotSupportedException();
                var demandConvertPriceCurrent = eDataContext.DemandConvertPrices.Where(ca => ca.ID == id).SingleOrDefault();
                var ListType = eDataContext.DemandTypes.ToList();
                return View(new DemandCreatePriceViewModels
                {
                    CurrentDemandConvertPrice = demandConvertPriceCurrent,
                    Max = (Decimal)demandConvertPriceCurrent.MaxValue,     // Vietlv change "(int)" => (Decimal)
                    Min = (Decimal)demandConvertPriceCurrent.MinValue,    // Vietlv change "(int)" => (Decimal)
                    DemandTypes = ListType,
                    CurrenTypeID = demandConvertPriceCurrent.TypeID,
                    Name = demandConvertPriceCurrent.Name
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region POST: /ConvertPrice/Edit
        [HttpPost]
        public ActionResult Edit(int id, DemandCreatePriceViewModels cp)
        {
            try
            {
                if (id < 0) throw new NotSupportedException();
                UpdateModel(cp);
                cp.CurrentDemandConvertPrice = eDataContext.DemandConvertPrices.Where(c => c.ID == id).SingleOrDefault();
                cp.CurrentDemandConvertPrice.TypeID = cp.CurrenTypeID;
                cp.CurrentDemandConvertPrice.Name = cp.Name;
                if (cp.Min > 0 && cp.Max > 0 && cp.Min > cp.Max)
                {

                    ViewData["MessageInfo"] = "Dữ liệu bị lỗi bạn hãy xem lại dữ liệu đầu vào";
                    return View();
                }
                if (cp.Min < 0 || cp.Max < 0)
                {
                    ViewData["MessageInfo"] = "Dữ liệu bị lỗi bạn hãy xem lại dữ liệu đầu vào";
                    return View();
                }
                cp.CurrentDemandConvertPrice.MinValue = cp.Min;
                cp.CurrentDemandConvertPrice.MaxValue = cp.Max;
                eDataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewData["MessageInfo"] = "Dữ liệu bị lỗi bạn hãy xem lại dữ liệu đầu vào";
                return View();
            }
        }
        #endregion

        #region GET: /ConvertPrice/Delete
        public ActionResult Delete(int id)
        {
            try
            {
                if (id < 0) throw new NotSupportedException();
                var CurrentDemandConvertPrice = eDataContext.DemandConvertPrices.Where(ca => ca.ID == id).SingleOrDefault();
                return View(CurrentDemandConvertPrice);
            }
            catch (Exception ex)
            {
                ViewData["MessageInfo"] = "Có lỗi xảy ra bạn không thể xóa được";
                return View();
            }
        }
        #endregion

        #region POST: /ConvertPrice/Delete
        [HttpPost]
        public ActionResult Delete(int id, FormCollection frm)
        {
            try
            {
                if (id < 0) throw new NotSupportedException();
                var CurrentDemandConvertPrice = eDataContext.DemandConvertPrices.Where(ca => ca.ID == id).SingleOrDefault();
                eDataContext.DemandConvertPrices.DeleteOnSubmit(CurrentDemandConvertPrice);
                eDataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewData["MessageInfo"] = "Có lỗi xảy ra bạn không thể xóa được";
                return View();
            }

        }
        #endregion

        #region GET: /ConvertPrice/Detail
        public ActionResult Detail(int id)
        {
            try
            {
                if (id < 0) throw new NotSupportedException();
                var ConvertPriceCurrent = eDataContext.DemandConvertPrices.Where(ca => ca.ID == id).SingleOrDefault();
                var ListType = eDataContext.DemandTypes.ToList();
                return View(new DemandCreatePriceViewModels
                {
                    CurrentDemandConvertPrice = ConvertPriceCurrent,
                    Max = (Decimal)ConvertPriceCurrent.MaxValue,    //Vietlv changes int to Decimal
                    Min = (Decimal)ConvertPriceCurrent.MinValue     //Vietlv changes int to Decimal
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
    public static class ExtentionNameDemandConvertPrice
    {
        public static DemandType NameProductType(this DemandConvertPrice cp)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                if (cp.TypeID < 0) throw new NotSupportedException();
                return eDataContext.DemandTypes.Where(p => p.ID == cp.TypeID).FirstOrDefault();
            }
            catch (Exception)
            { throw; }
            finally
            {
                eDataContext.Dispose();
            }
        }
        public static MembershipUser NameUserAddBy(this DemandConvertPrice cp)
        {
            try
            {
                if (string.IsNullOrEmpty(cp.AddBy.ToString())) throw new NotSupportedException();
                return UserManager.GetUser(cp.AddBy, true);
            }
            catch (Exception)
            { throw; }

        }
    }
}
